package com.oocl.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import com.oocl.global.GlobalVar;
import com.oocl.interaction.Chater;

public class TcpServer {
	private Socket tsk;
	private ServerSocket tcpServer;
	
	public TcpServer(Socket tsk, ServerSocket tcpServer) {
		super();
		this.tsk = tsk;
		this.tcpServer = tcpServer;
	}

	public void startTcpServer () {
		new Thread() {
			public void run() {
				try {
					while (true) {
						try {
							tsk = tcpServer.accept();
							GlobalVar.TcpServerStatus = true;
							new Thread(new Chater(tsk)).start();
							System.out.println("TcpServerStatus: " + GlobalVar.TcpServerStatus);
						} catch (SocketException e) {
							tsk.close();
							tcpServer.close();
							GlobalVar.TcpServerStatus = false;
							break;
						}
					}	
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		}.start();
	}
	
	public void stopTcpServer () throws IOException {
		//tsk.close();
		tcpServer.close();
		GlobalVar.TcpServerStatus = false;
	}
}
